package com.ruoyi.ipms_crm.service.impl;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IPMSUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ipms_crm.dto.*;
import com.ruoyi.ipms_crm.entity.Customer;
import com.ruoyi.ipms_crm.mapper.CustomerMapper;
import com.ruoyi.ipms_crm.service.CustomerService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*客户业务层实现类*/
@Service
@SuppressWarnings("all")
public class CustomerServiceImpl implements CustomerService {

    @Autowired(required = false)
    //RedisCache redisCache;

    @Resource
    private CustomerMapper customerMapper;

    /**
     * 根据客户id查询客户详情
     * @param customerId
     * @return
     */
    @Override
    public Customer selectCustomerById(Integer customerId) {
        return customerMapper.selectCustomerById(customerId);

    }

    /**
     * 查询业务员客户列表
     * @param customer
     * @return
     */
    @Override
    public List<Customer> SelectCustomerListByUserId(Customer customer) {
        customer.setIsDelete("0");
        //获取当前用户信息
        LoginUser loginUser= SecurityUtils.getLoginUser();
        int userid = Integer.parseInt(loginUser.getUserid()+"");
        customer.setUserId(userid);
        customer.setCustomerStatusId("0");
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 查询客户公海列表
     * @param customer
     * @return
     */
    @Override
    public List<Customer> selectListByCustomerSeas(Customer customer) {
        customer.setIsDelete("0");
        customer.setCustomerStatusId("1");
        return customerMapper.selectCustomerList(customer);
    }


    /**
     * 新增客户
     * @param customer
     * @return
     */
    @Override
    public int insertCustomer(Customer customer) {
        LoginUser loginUser= SecurityUtils.getLoginUser();

        /*客户编号*/
        customer.setCustomerNo(IPMSUtils.spawnNo("CUS.",5));

        /*绑定员工id*/
        customer.setUserId(Integer.parseInt(loginUser.getUserid()+""));

        /*计数置0*/
        customer.setCustomerExperience(0);

        //创建时间
        customer.setCreateTime(DateUtils.getNowDate());
        //乐观锁
        customer.setVersion(Constants.VERSION_LOCK);
        //逻辑删除
        customer.setIsDelete(Constants.IS_DELETED);
        //创建人
        customer.setCreateBy(loginUser.getSysUser().getNickName());
        //更新人
        customer.setUpdateBy(loginUser.getSysUser().getNickName());
        //更新时间
        customer.setUpdateTime(DateUtils.getNowDate());
        //调用mapper
        customerMapper.insertCustomer(customer);
        return customer.getCustomerId();
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @Override
    public int updateCustomerById(Customer customer) {
        //获取登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获取被修改客户Id
        Customer oldCustomer = customerMapper.selectCustomerById(customer.getCustomerId());
        //修改更新时间
        customer.setUpdateTime(DateUtils.getNowDate());
        //修改更新人
        customer.setUpdateBy(loginUser.getUsername());
        //获取该员工乐观锁
        int version = oldCustomer.getVersion();
        version++;
        //修改乐观锁次数
        customer.setVersion(version);
        return customerMapper.updateCustomerById(customer);
    }

    /**
     * 逻辑删除客户
     * @param customerId
     * @return
     */
    @Override
    public int deleteCustomerByIds(Integer[] customerId) {
        return customerMapper.deleteCustomerByIds(customerId);
    }

    @Override
    public CustomerDetailsDto selectCustomerDetails(Integer customerId) {
        return customerMapper.selectCustomerDetails(customerId);
    }

    @Override
    public List<CustomerAndInventorDto> selectInventorForCustomer(Integer customerId) {
        return customerMapper.selectInventorForCustomer(customerId);
    }

    @Override
    public List<CustomerAndApplicationDto> selectApplicantForCustomer(Integer customerId) {
        return customerMapper.selectApplicantForCustomer(customerId);
    }

    @Override
    public List<CustomerOfContractDto> selectContractForCustomer(Integer customerId) {
        return customerMapper.selectContractForCustomer(customerId);
    }

    @Override
    public List<CustomerOfCaseDto> selectCaseForCustomer(Integer customerId) {
        return customerMapper.selectCaseForCustomer(customerId);
    }
    /**
     * 查询客户回收站列表
     * @param customer
     * @return
     */
    @Override
    public List<Customer> selectRecycleCustomerList(Customer customer) {
        return customerMapper.selectRecycleCustomerList(customer);
    }

    @Override
    public boolean updateCustomer(List customerIds) {
        return customerMapper.updateCustomer(customerIds)>0;
    }


}
